package centerservice

import (
	"admin100/app/dao/centerdao"
	"admin100/app/model/centermodel"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/utils"
	"strings"

	"github.com/gogf/gf/frame/g"
)

var CGameServer = cGameService{}

type cGameService struct{}

// 获取所有游戏节点
func (s *cGameService) FetchAllNodeByPlatId(ptId string) []string {
	if ptId == "" {
		return []string{}
	}
	data, err := centerdao.CGameServer.Fields(centerdao.CGameServer.Columns.Node).Where(g.Map{
		centerdao.CGameServer.Columns.PlatformId: ptId,
	}).FindAll()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	var res = make([]string, 0)
	for _, v := range data {
		res = append(res, v.Node)
	}

	return utils.RemoveRedata(res)
}

// 通过平台id和区服id获取node列表
func (s *cGameService) FetchNodeSliceByPtIdAndSid(ptId string, sid []string) []string {
	if ptId == "" || len(sid) == 0 {
		return []string{}
	}
	data, err := centerdao.CGameServer.Fields(centerdao.CGameServer.Columns.Node).Where(g.Map{
		centerdao.CGameServer.Columns.PlatformId: ptId,
		centerdao.CGameServer.Columns.Sid:        sid,
	}).FindAll()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	var res = make([]string, 0)
	for _, v := range data {
		res = append(res, v.Node)
	}

	return utils.RemoveRedata(res)
}

// 通过平台id和区服id获取node
func (s *cGameService) FetchNodeStrByPtIdAndSid(ptId, sid string) string {
	if ptId == "" || sid == "" {
		return ""
	}

	res, err := centerdao.CGameServer.Fields(centerdao.CGameServer.Columns.Node).Where(g.Map{
		centerdao.CGameServer.Columns.PlatformId: ptId,
		centerdao.CGameServer.Columns.Sid:        sid,
	}).FindOne()
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}

	if res != nil {
		return res.Node
	}
	return ""
}

func (s *cGameService) GetServerList(platformIdList []string) []interface{} {
	var serverList = make([]interface{}, 0)
	for _, platformId := range platformIdList {
		gameServerList, err := centerdao.CGameServer.FieldsEx(
			centerdao.CGameServer.Columns.IsShow).Where(centerdao.CGameServer.Columns.PlatformId, platformId).FindAll()
		defErr.IfPanic(err, define.DataOptFail)
		for _, v := range gameServerList {
			serverList = append(serverList, struct {
				PlatformId string
				Sid        string
				Desc       string
				Node       string
			}{
				PlatformId: v.PlatformId,
				Sid:        v.Sid,
				Desc:       v.Desc,
				Node:       v.Node,
			})
		}
	}

	return serverList
}

// getOne 是否只获取一条
func (s *cGameService) GetGameServerList(platformId string, sid []string, getOne bool) ([]*centermodel.CGameServer, *centermodel.CGameServer) {
	list, err := centerdao.CGameServer.Where(g.Map{
		centerdao.CGameServer.Columns.PlatformId: platformId,
		centerdao.CGameServer.Columns.Sid:        sid,
	}).FindAll()
	defErr.IfPanic(err, define.RecordNodFound)
	if list == nil {
		defErr.Panic(define.RecordNodFound)
	}

	if len(list) > 0 && getOne {
		return nil, list[0]
	}

	return list, nil
}

// func (s *cGameService) GetGameServerOne(platformId, sid string) *centermodel.CGameServer {
// 	r, err := centerdao.CGameServer.Where(g.Map{
// 		centerdao.CGameServer.Columns.PlatformId: platformId,
// 		centerdao.CGameServer.Columns.Sid:        sid,
// 	}).FindOne()
// 	defErr.IfPanic(err, define.RecordNodFound)
// 	if r == nil {
// 		defErr.Panic(define.RecordNodFound)
// 	}

// 	return r
// }
func (s *cGameService) GetServerNode(node string) *centermodel.CGameServer {
	r, err := centerdao.CGameServer.Where(centerdao.CGameServer.Columns.Node, node).FindOne()
	defErr.IfPanic(err, define.DataOptFail)
	if r == nil {
		return nil
	}
	return r
}

func (s *cGameService) GetGameServerIdListStringByNode(node string) string {
	r, err := centerdao.CGameServer.Where(centerdao.CGameServer.Columns.Node, node).FindAll()
	defErr.IfPanic(err, define.DataOptFail)
	if r == nil {
		return ""
	}

	serverIdList := make([]string, 0)
	for _, v := range r {
		serverIdList = append(serverIdList, v.Sid)
	}

	return strings.Join(serverIdList, ",")
}
